﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace CDSysLDManageProject
{
    public partial class WCS : Form
    {
        static SocketTest st1 = new SocketTest();

        string IP = System.Configuration.ConfigurationManager.AppSettings["WCSAddress"];
        int IPPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WCSPort"]);
        public WCS()
        {
            InitializeComponent();
            

        }

        string fromx = "";     //32
        string froml = "";     //33
        string fromc = "";     //34
        bool hadOutInventory = false;
        bool hadInInventory = false;
        string tox = "";        //35
        string tol = "";        //36
        string toc = "";        //37

        //校验并发送读指令
        public string ReadRightValue(string  eqadr, int eqno, string expectvalue)
        {
            int ngcount = 1;
            string recvalue = "";

            if (expectvalue != "notnull")
            {
                while (ngcount <= 3 && recvalue != expectvalue)
                {
                    if (st1.Connected() == false)
                    {
                        msgwarn.Text = "网络连接断开";
                        msgwarn.BackColor = Color.Red;
                        WriteLogTxt(DateTime.Now + " " + "网络连接断开");
                        while (st1.Connected() == false)
                        {
                            ReConnect();
                        }

                    }
                    else if (st1.Connected() == true)
                    {
                        bool rdflag = st1.sendRddata(eqadr, eqno);
                        if (!rdflag)
                        {
                            WriteLogTxt(DateTime.Now + "  读取" + eqadr + " 寄存器失败");
                        }

                        Thread.Sleep(100);
                        recvalue = st1.receivedata(eqno, "r");
                        ngcount++;

                    }
                    

                }
            }
            else
            {
                while (ngcount <= 3 && recvalue == "")
                {
                    if (st1.Connected() == false)
                    {
                        msgwarn.Text = "网络连接断开";
                        msgwarn.BackColor = Color.Red;
                        WriteLogTxt(DateTime.Now + " " + "网络连接断开");
                        while (st1.Connected() == false)
                        {
                            ReConnect();
                        }
                    }
                    else if (st1.Connected())
                    {
                        bool rdflag = st1.sendRddata(eqadr, eqno);
                        if (!rdflag)
                        {
                            WriteLogTxt(DateTime.Now + "  读取" + eqadr + " 寄存器失败");
                        }
                        Thread.Sleep(200);
                        recvalue = st1.receivedata(eqno, "r");
                        ngcount++;
                    }

                }
            }

            Thread.Sleep(200);
            return recvalue;

        }


        //校验并发送写指令
        public void WriteRightValue(int eqadr, int eqno, int value)
        {
            int ngcount = 1;
            string recvalue = "";
            bool sendflag = false;

            while (sendflag != true)
            {
                if (st1.Connected() == false)
                {
                    msgwarn.Text = "网络连接断开";
                    msgwarn.BackColor = Color.Red;
                    WriteLogTxt(DateTime.Now + " 网络连接断开");
                    while (st1.Connected() == false)
                    {
                        ReConnect();
                    }

                }
                else if (st1.Connected())
                {
                    sendflag = st1.sendWrdata(eqadr, eqno, value);
                    if (!sendflag)
                    {
                        WriteLogTxt(DateTime.Now + "  " + eqadr + "寄存器写" + value + "失败");

                    }
                    Thread.Sleep(100);
                    recvalue = st1.receivedata(1, "w");
                    ngcount++;

                }

            }
            Thread.Sleep(200);
          
        }



        //连接网络
        private void ReConnect()
        {
            if (!st1.Connected())
            {
                try
                {
                    st1.CreateClient(IP, IPPort);
                }
                catch (Exception)
                {


                }

            }
            else
            {

                msgwarn.Text = "网络连接恢复";
                msgwarn.BackColor = Color.SpringGreen;
                return;
            }
        }

        //出库动作
        private void button3_Click(object sender, EventArgs e)
        {
            bgBeginProcess.RunWorkerAsync();
            
            //bgReplayStageIn.RunWorkerAsync();

        }



        //创建txt     将测定结果存到指定路径下
        public void WriteLogTxt(string errorstr)
        {

            string exportPath = System.AppDomain.CurrentDomain.BaseDirectory+"\\WCSLog"; //生成LOG路径

            if (!Directory.Exists(exportPath))
            {
                Directory.CreateDirectory(exportPath);
            }
            string NewFileName = "ErrorLog.txt";
            exportPath = exportPath + "\\" + NewFileName;
            
            if (!File.Exists(exportPath))
            {

                File.Create(exportPath).Dispose();

            }
            else
            {
                FileStream fs1 = new FileStream(exportPath, FileMode.Append, FileAccess.Write, FileShare.Read);//创建写入文件 
                StreamWriter sw = new StreamWriter(fs1);

                sw.WriteLine(errorstr);//开始写入值

                sw.Close();
                fs1.Close();
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
             fromx = tbxfrom.Text.Substring(0,1);     //32
             froml = tbxfrom.Text.Substring(2,1);     //33
             fromc = tbxfrom.Text.Substring(4,1);     //34

             tox = tbxto.Text.Substring(0, 1);        //35
             tol = tbxto.Text.Substring(2, 1);        //36
             toc = tbxto.Text.Substring(4, 1);        //37

            string rec593 = ReadRightValue("593",1,"0001");
            if (rec593 == "0001")
            {
                string rec590 = ReadRightValue("590",1,tox.PadLeft(4,'0'));
                string rec591 = ReadRightValue("591", 1, tol.PadLeft(4, '0'));
                string rec592 = ReadRightValue("592", 1, toc.PadLeft(4, '0'));
                if (rec590 == tox.PadLeft(4, '0') && rec591 == tol.PadLeft(4, '0') && rec592 == toc.PadLeft(4, '0'))
                {
                   
                    

                }
                else
                {
                    msgwarn.Text = "入库位置不正确";
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            fromx = tbxfrom.Text.Substring(0, 1);     //32
            froml = tbxfrom.Text.Substring(2, 1);     //33
            fromc = tbxfrom.Text.Substring(4, 1);     //34

            tox = tbxto.Text.Substring(0, 1);        //35
            tol = tbxto.Text.Substring(2, 1);        //36
            toc = tbxto.Text.Substring(4, 1);        //37

            string rec593 = ReadRightValue("593", 1, "0002");
            if (rec593 == "0002")
            {
                string rec590 = ReadRightValue("590", 1, fromx.PadLeft(4, '0'));
                string rec591 = ReadRightValue("591", 1, froml.PadLeft(4, '0'));
                string rec592 = ReadRightValue("592", 1, fromc.PadLeft(4, '0'));
                if (rec590 == fromx.PadLeft(4, '0') && rec591 == froml.PadLeft(4, '0') && rec592 == fromc.PadLeft(4, '0'))
                {

                    WriteRightValue(90, 1, int.Parse(fromx.PadLeft(4, '0')));
                    WriteRightValue(91, 1, int.Parse(froml.PadLeft(4, '0')));
                    WriteRightValue(92, 1, int.Parse(fromc.PadLeft(4, '0')));
                    WriteRightValue(93, 1, 2);
                    if (ReadRightValue("593", 1, "0000") == "0000")
                    {
                        WriteRightValue(93, 1, 0);
                    }

                }
                else
                {
                    msgwarn.Text = "出库位置不正确";
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            WriteRightValue(10,1,0);
            WriteRightValue(11, 1, 0);
            
            //复位F/T
            WriteRightValue(30, 1, 0);
            WriteRightValue(31, 1, 0);
            WriteRightValue(32, 1, 0);
            WriteRightValue(33, 1, 0);
            WriteRightValue(34, 1, 0);
            WriteRightValue(35, 1, 0);
            WriteRightValue(36, 1, 0);
            WriteRightValue(37, 1, 0);

            //复位StagePin
            WriteRightValue(90, 1, 0);
            WriteRightValue(91, 1, 0);
            WriteRightValue(92, 1, 0);
            WriteRightValue(93, 1, 0);

            hadInInventory = false;
            hadOutInventory = false;
            if (ReadRightValue("511", 1, "0001") == "0001")
            {
                msgwarn.Text = "W511状态为1,需要复位";
                msgwarn.BackColor = Color.SpringGreen;
                WriteRightValue(11, 1, 1);
                Thread.Sleep(200);
                if (ReadRightValue("511", 1, "0000") == "0000")
                {
                    WriteRightValue(11, 1, 0);
                    msgwarn.Text = "已成功复位11为0";
                    msgwarn.BackColor = Color.SpringGreen;

                    Thread.Sleep(200);
                }
            }
            else
            {
                msgwarn.Text = "W511状态不为1,无需复位";
                msgwarn.BackColor = Color.SpringGreen;
            }

        }

        //执行入库动作
        private void button5_Click(object sender, EventArgs e)
        {
            string rec506 = ReadRightValue("506", 1, "0001");
            if (rec506 != "0001")
            {
                return;
            }
            fromx = tbxfrom.Text.Substring(0, 1);     //32
            froml = tbxfrom.Text.Substring(2, 1);     //33
            fromc = tbxfrom.Text.Substring(4, 1);     //34

            tox = tbxto.Text.Substring(0, 1);        //35
            tol = tbxto.Text.Substring(2, 1);        //36
            toc = tbxto.Text.Substring(4, 1);        //37

            //动作指示ID
            WriteRightValue(30, 1, 6);

            //动作类型
            int ActionType = int.Parse(tbxActionType.Text);
            WriteRightValue(31, 1, 4);

            //From 
            WriteRightValue(32, 1, int.Parse(fromx));
            WriteRightValue(33, 1, int.Parse(froml));
            WriteRightValue(34, 1, int.Parse(fromc));

            //To的值
            WriteRightValue(35, 1, int.Parse(tox));
            WriteRightValue(36, 1, int.Parse(tol));
            WriteRightValue(37, 1, int.Parse(toc));

            //更新
            WriteRightValue(10, 1, 1);

            WriteRightValue(100, 1, 1);
            //等待W0510是否置1

            string rec510 = ReadRightValue("510", 1, "0001");

            if (rec510 == "0001")
            {
                WriteRightValue(10, 1, 0);
                string ldstatus = "";

                while (ReadRightValue("511", 1, "0001") != "0001")
                {
                    ldstatus = ReadRightValue("507", 1, "notnull");
                    if (ldstatus == "0002")
                    {
                        msgwarn.Text = "LD运行开始";
                    }
                    else if (ldstatus == "0003")
                    {
                        msgwarn.Text = "LD到达 装载开始";
                    }
                    else if (ldstatus == "0004")
                    {
                        msgwarn.Text = "LD装载下降地运行开始";
                    }
                    else if (ldstatus == "0005")
                    {
                        msgwarn.Text = "LD到达 装载下降开始";
                    }

                }

                string rec560 = ReadRightValue("560", 1, "0006");
                string rec561 = ReadRightValue("561", 1, tbxActionType.Text.PadLeft(4, '0'));

                string rec562 = ReadRightValue("562", 1, fromx.PadLeft(4, '0'));
                string rec563 = ReadRightValue("563", 1, froml.PadLeft(4, '0'));
                string rec564 = ReadRightValue("564", 1, fromc.PadLeft(4, '0'));

                string rec565 = ReadRightValue("565", 1, tox.PadLeft(4, '0'));
                string rec566 = ReadRightValue("566", 1, tol.PadLeft(4, '0'));
                string rec567 = ReadRightValue("567", 1, toc.PadLeft(4, '0'));

                string endcode = ReadRightValue("568", 1, "notnull");

                if (endcode == "0001")
                {
                    if (rec560 == "0006" && rec561 == tbxActionType.Text.PadLeft(4, '0') && rec562 == fromx.PadLeft(4, '0') && rec563 == froml.PadLeft(4, '0') && rec564 == fromc.PadLeft(4, '0') && rec565 == tox.PadLeft(4, '0') && rec566 == tol.PadLeft(4, '0') && rec567 == toc.PadLeft(4, '0'))
                    {
                        msgwarn.Text = "堆垛机己将托盘运送到指定职位，请点击入库或出库";
                    }
                    else
                    {
                        msgwarn.Text = "堆垛机未运送到正确位置";
                    }


                }
                else if (endcode == "0002")
                {
                    msgwarn.Text = "堆垛机运行异常";
                }

                else if (endcode == "0003")
                {
                    msgwarn.Text = "堆垛机运行未结束(无装载)";
                }

                else if (endcode == "0004")
                {
                    msgwarn.Text = "堆垛机运行未结束(有装载)";
                }

            }
            else
            {
                WriteLogTxt(DateTime.Now + " PLC响应异常");
                msgwarn.Text = "PLC响应异常";
                msgwarn.BackColor = Color.Red;
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            
        }

        private void button6_Click(object sender, EventArgs e)
        {
           
            WriteRightValue(11, 1, 1);
            
            
            Thread.Sleep(100);
            string rec511 = ReadRightValue("511", 1, "0000");
            if (rec511 == "0000")
            {
                WriteRightValue(11, 1, 0);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {

            tbxfrom.Text = tbxto.Text;
            tbxto.Text = tbxfrom.Text;
           
            
        }

        private void bgBeginProcess_DoWork(object sender, DoWorkEventArgs e)
        {
            string rec506 = ReadRightValue("506", 1, "0001");
            if (rec506 != "0001")
            {
                return;
            }
            try
            {
                fromx = tbxfrom.Text.Substring(0, 1);     //32
                froml = tbxfrom.Text.Substring(2, 1);     //33
                fromc = tbxfrom.Text.Substring(4, 1);     //34

                tox = tbxto.Text.Substring(0, 1);        //35
                tol = tbxto.Text.Substring(2, 1);        //36
                toc = tbxto.Text.Substring(4, 1);        //37
            }
            catch (Exception)
            {

                tbmsg.Text = "请输入正确的出入库位置";
                tbmsg.BackColor = Color.Red;
                return;
            }
            //动作指示ID
            WriteRightValue(30, 1, 6);

            //动作类型
            int ActionType = int.Parse(tbxActionType.Text);
            WriteRightValue(31, 1, 4);

            //From 
            WriteRightValue(32, 1, int.Parse(fromx));
            WriteRightValue(33, 1, int.Parse(froml));
            WriteRightValue(34, 1, int.Parse(fromc));

            //To的值
            WriteRightValue(35, 1, int.Parse(tox));
            WriteRightValue(36, 1, int.Parse(tol));
            WriteRightValue(37, 1, int.Parse(toc));

            //更新
            WriteRightValue(10, 1, 1);

            WriteRightValue(100, 1, 1);
            //等待W0510是否置1

            string rec510 = ReadRightValue("510", 1, "0001");

            if (rec510 == "0001")
            {
                msgwarn.Text = "应答流程完成，老司机即将飚车";
                msgwarn.BackColor = Color.SpringGreen;
                WriteRightValue(10, 1, 0);
                string ldstatus = "";

                while (ReadRightValue("511", 1, "notnull") != "0001")
                {


                    if (tbxfrom.Text != "" && tbxto.Text != "")
                    {
                        try
                        {
                            fromx = tbxfrom.Text.Substring(0, 1);     //32
                            froml = tbxfrom.Text.Substring(2, 1);     //33
                            fromc = tbxfrom.Text.Substring(4, 1);     //34
                            tox = tbxto.Text.Substring(0, 1);        //35
                            tol = tbxto.Text.Substring(2, 1);        //36
                            toc = tbxto.Text.Substring(4, 1);        //37
                        }
                        catch (Exception)
                        {

                        }

                        string rec590 = ReadRightValue("590", 1, "notnull");
                        Thread.Sleep(200);
                        string rec591 = ReadRightValue("591", 1, "notnull");
                        Thread.Sleep(200);
                        string rec592 = ReadRightValue("592", 1, "notnull");
                        Thread.Sleep(200);
                        //如果未出库的话才出库
                        if (hadOutInventory == false)
                        {
                            if (rec590 == fromx.PadLeft(4, '0') && rec591 == froml.PadLeft(4, '0') && rec592 == fromc.PadLeft(4, '0'))
                            {

                                msgwarn.Text = "堆垛机出库" + rec590 + "-" + rec591 + "-" + rec592 + "已就位,请 Request Uploading Reservation";
                                msgwarn.BackColor = Color.SpringGreen;
                                WriteRightValue(90, 1, int.Parse(fromx.PadLeft(4, '0')));
                                Thread.Sleep(200);
                                WriteRightValue(91, 1, int.Parse(froml.PadLeft(4, '0')));
                                Thread.Sleep(200);
                                WriteRightValue(92, 1, int.Parse(fromc.PadLeft(4, '0')));
                                Thread.Sleep(200);
                                //

                                string rec593 = ReadRightValue("593", 1, "0002");
                                Thread.Sleep(200);

                                if (rec593 == "0002")
                                {
                                    msgwarn.Text = "Fork已经抓取到托盘,即将收回";
                                    msgwarn.BackColor = Color.SpringGreen;
                                    WriteRightValue(93, 1, 2);
                                    Thread.Sleep(500);
                                    if (ReadRightValue("593", 1, "0000") == "0000")
                                    {
                                        WriteRightValue(93, 1, 0);
                                        msgwarn.Text = "堆垛机Fork已收回";
                                        msgwarn.BackColor = Color.SpringGreen;
                                        hadOutInventory = true;
                                        Thread.Sleep(500);
                                        msgwarn.Text = "出库完成，老司机在载客中!";
                                        msgwarn.BackColor = Color.SpringGreen;
                                    }
                                }
                                

                            }
                        }
                        else if (rec590 == tox.PadLeft(4, '0') && rec591 == tol.PadLeft(4, '0') && rec592 == toc.PadLeft(4, '0'))
                        {

                            if (hadInInventory == false)
                            {
                                msgwarn.Text = "堆垛机入库" + rec590 + "-" + rec591 + "-" + rec592 + "已就位,请Send Loading Complete";
                                msgwarn.BackColor = Color.SpringGreen;
                                Thread.Sleep(500);

                                //先验证入库位置是否正确


                                WriteRightValue(90, 1, int.Parse(tox.PadLeft(4, '0')));
                                Thread.Sleep(200);
                                WriteRightValue(91, 1, int.Parse(tol.PadLeft(4, '0')));
                                Thread.Sleep(200);
                                WriteRightValue(92, 1, int.Parse(toc.PadLeft(4, '0')));
                                Thread.Sleep(200);
                                //
                                string rec593 = ReadRightValue("593", 1, "0001");
                                Thread.Sleep(200);
                                if (rec593 == "0001")
                                {
                                    msgwarn.Text = "堆垛机Fork将托盘推入架台!";
                                    msgwarn.BackColor = Color.SpringGreen;
                                    Thread.Sleep(500);
                                    WriteRightValue(93, 1, 1);
                                    Thread.Sleep(200);
                                    if (ReadRightValue("593", 1, "0000") == "0000")
                                    {
                                        WriteRightValue(93, 1, 0);
                                        msgwarn.Text = "堆垛机已完成入库动作";
                                        msgwarn.BackColor = Color.SpringGreen;
                                        Thread.Sleep(200);
                                        hadInInventory = true;
                                        msgwarn.Text = "入库完成，堆垛机即将结束工作！！";
                                        msgwarn.BackColor = Color.SpringGreen;
                                        //
                                    }
                                }
                            }

                        }


                        ldstatus = ReadRightValue("507", 1, "notnull");
                        if (ldstatus == "0002")
                        {
                            msgwarn.Text = "LD运行开始";
                            msgwarn.BackColor = Color.SpringGreen;
                        }
                        else if (ldstatus == "0003")
                        {
                            msgwarn.Text = "LD到达 装载开始";
                            msgwarn.BackColor = Color.SpringGreen;
                        }
                        else if (ldstatus == "0004")
                        {
                            msgwarn.Text = "LD装载下降地运行开始";
                            msgwarn.BackColor = Color.SpringGreen;
                        }
                        else if (ldstatus == "0005")
                        {
                            msgwarn.Text = "LD到达 装载下降开始";
                            msgwarn.BackColor = Color.SpringGreen;
                        }

                    }
                    Thread.Sleep(200);
                }
                //复位
                WriteRightValue(11, 1, 1);
                Thread.Sleep(200);
                if (ReadRightValue("511", 1, "0000") == "0000")
                {
                    WriteRightValue(11, 1, 0);
                    msgwarn.Text = "堆垛机已经完成整个动作！！！";
                    msgwarn.BackColor = Color.SpringGreen;
                    

                }


                if (hadInInventory == true)
                {
                    string rec560 = ReadRightValue("560", 1, "0006");
                    Thread.Sleep(200);
                    string rec561 = ReadRightValue("561", 1, tbxActionType.Text.PadLeft(4, '0'));
                    Thread.Sleep(200);
                    string rec562 = ReadRightValue("562", 1, fromx.PadLeft(4, '0'));
                    Thread.Sleep(200);
                    string rec563 = ReadRightValue("563", 1, froml.PadLeft(4, '0'));
                    Thread.Sleep(200);
                    string rec564 = ReadRightValue("564", 1, fromc.PadLeft(4, '0'));
                    Thread.Sleep(200);
                    string rec565 = ReadRightValue("565", 1, tox.PadLeft(4, '0'));
                    Thread.Sleep(200);
                    string rec566 = ReadRightValue("566", 1, tol.PadLeft(4, '0'));
                    Thread.Sleep(200);
                    string rec567 = ReadRightValue("567", 1, toc.PadLeft(4, '0'));
                    Thread.Sleep(200);
                    string endcode = ReadRightValue("568", 1, "notnull");
                    Thread.Sleep(200);

                    if (endcode == "0001")
                    {
                        if (rec560 == "0006" && rec561 == tbxActionType.Text.PadLeft(4, '0') && rec562 == fromx.PadLeft(4, '0') && rec563 == froml.PadLeft(4, '0') && rec564 == fromc.PadLeft(4, '0') && rec565 == tox.PadLeft(4, '0') && rec566 == tol.PadLeft(4, '0') && rec567 == toc.PadLeft(4, '0'))
                        {
                            msgwarn.Text = "堆垛机己将托盘运送到指定位置";
                        }
                        else
                        {
                            msgwarn.Text = "堆垛机未运送到正确位置";
                        }


                    }
                    else if (endcode == "0002")
                    {
                        msgwarn.Text = "堆垛机运行异常";
                    }

                    else if (endcode == "0003")
                    {
                        msgwarn.Text = "堆垛机运行未结束(无装载)";
                    }

                    else if (endcode == "0004")
                    {
                        msgwarn.Text = "堆垛机运行未结束(有装载)";
                    }
                }
                else
                {

                    msgwarn.Text = "堆垛机没入库，但是w511却为1 ！！！";
                    msgwarn.BackColor = Color.Red;
                }

            }
            else
            {
                WriteLogTxt(DateTime.Now + " PLC响应异常");
                msgwarn.Text = "PLC响应异常";
                msgwarn.BackColor = Color.Red;
            }


            hadInInventory = false;
            hadOutInventory = false;
        }
        private void button8_Click(object sender, EventArgs e)
        {
            bgReplayStageIn.RunWorkerAsync();
        }

        private void WCS_Load(object sender, EventArgs e)
        {
            //bgReplayStageIn.RunWorkerAsync();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            st1.CreateClient(IP, IPPort);
            
        }

        private void bgReplayStageIn_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {

                if (tbxfrom.Text != "" && tbxto.Text != "")
                {
                    fromx = tbxfrom.Text.Substring(0, 1);     //32
                    froml = tbxfrom.Text.Substring(2, 1);     //33
                    fromc = tbxfrom.Text.Substring(4, 1);     //34
                    tox = tbxto.Text.Substring(0, 1);        //35
                    tol = tbxto.Text.Substring(2, 1);        //36
                    toc = tbxto.Text.Substring(4, 1);        //37
                    string rec590 = ReadRightValue("590", 1, "notnull");
                    string rec591 = ReadRightValue("591", 1, "notnull");
                    string rec592 = ReadRightValue("592", 1, "notnull");
                    if (rec590 == fromx.PadLeft(4, '0') && rec591 == froml.PadLeft(4, '0') && rec592 == fromc.PadLeft(4, '0'))
                    {
                        msgwarn.Text = "堆垛机出库" + rec590 + "-" + rec591 + "-" + rec592 + "已就位,请 Request Uploading Reservation";
                        msgwarn.BackColor = Color.SpringGreen;
                        WriteRightValue(90, 1, int.Parse(fromx.PadLeft(4, '0')));
                        WriteRightValue(91, 1, int.Parse(froml.PadLeft(4, '0')));
                        WriteRightValue(92, 1, int.Parse(fromc.PadLeft(4, '0')));
                        //
                        Thread.Sleep(100);
                        string rec593 = ReadRightValue("593", 1, "0002");
                        Thread.Sleep(100);
                        if (rec593 == "0002")
                        {
                            //现在是出库中，请通知ECS销钉下降


                            WriteRightValue(93, 1, 2);
                            Thread.Sleep(100);
                            if (ReadRightValue("593", 1, "0000") == "0000")
                            {
                                WriteRightValue(93, 1, 0);
                                msgwarn.Text = "堆垛机已出库";
                                msgwarn.BackColor = Color.SpringGreen;
                            }
                        }

                    }
                    else if (rec590 == tox.PadLeft(4, '0') && rec591 == tol.PadLeft(4, '0') && rec592 == toc.PadLeft(4, '0'))
                    {
                        msgwarn.Text = "堆垛机入库" + rec590 + "-" + rec591 + "-" + rec592 + "已就位,请Send Loading Complete";
                        msgwarn.BackColor = Color.SpringGreen;
                        //tox = tbxto.Text.Substring(0, 1);        //35
                        //tol = tbxto.Text.Substring(2, 1);        //36
                        //toc = tbxto.Text.Substring(4, 1);        //37

                        //先验证入库位置是否正确

                        if (rec590 == tox.PadLeft(4, '0') && rec591 == tol.PadLeft(4, '0') && rec592 == toc.PadLeft(4, '0'))
                        {

                            WriteRightValue(90, 1, int.Parse(tox.PadLeft(4, '0')));
                            WriteRightValue(91, 1, int.Parse(tol.PadLeft(4, '0')));
                            WriteRightValue(92, 1, int.Parse(toc.PadLeft(4, '0')));
                            //
                            string rec593 = ReadRightValue("593", 1, "0001");
                            Thread.Sleep(100);
                            if (rec593 == "0001")
                            {
                                //现在是入库，请通知ECS销钉上升


                                WriteRightValue(93, 1, 1);
                                Thread.Sleep(100);
                                if (ReadRightValue("593", 1, "0000") == "0000")
                                {
                                    WriteRightValue(93, 1, 0);
                                    msgwarn.Text = "堆垛机已入库";
                                    msgwarn.BackColor = Color.SpringGreen;
                                }
                            }


                        }
                        else
                        {
                            msgwarn.Text = "出库位置不正确";
                        }
                    }
                }
            }
        }


        

    }
}
